package action.resource;

import java.util.List;

import model.Link;
import model.Page;
import model.Resource;
import model.User;

import DAO.ResourceDAO;
import DAO.DAOImple.ResourceImple;

import com.opensymphony.xwork2.ActionSupport;

public class LinkAction extends ActionSupport {
	private Link link;
	private List<Link> links;
	private List<Resource> resources;
	private Resource resource;
	private User user;
	
	private Page page;
	
	public String execute() {
		
		return SUCCESS;
	}
	
	/*
	 * 获得链接列表
	 */
	public String getLinkList() {
		ResourceDAO resourceDAO = new ResourceImple();
		if (page == null) {
			page = new Page();
			page.setCurrentPage(1);
		}
		page = page.getPage("T_Links where state = 0", page.getCurrentPage(), 20); // 获得分页对象
		links = resourceDAO.getLinkList(page);
		
		return SUCCESS;
	}
	
	/*
	 * 增加链接
	 */
	public String addLink() {
		ResourceDAO resourceDAO = new ResourceImple();
		int result = resourceDAO.addLink(link);
		if(result == 0) {  //失败
			 
		}else {  //成功
			if (page == null) {
				page = new Page();
				page.setCurrentPage(1);
			}
			page = page.getPage("T_Links where state = 0", page.getCurrentPage(), 20); // 获得分页对象
			links = resourceDAO.getLinkList(page);
		}
		return SUCCESS;
	}

	/*
	 * 修改链接
	 */
	public String modifyLink() {
		ResourceDAO resourceDAO = new ResourceImple();
		int result = resourceDAO.modifyLink(link);
		if(result == 0) {   //失败
			
		}else {   //成功
			
		}
		if (page == null) {
			page = new Page();
			page.setCurrentPage(1);
		}
		page = page.getPage("T_Links where state = 0", page.getCurrentPage(), 20); // 获得分页对象
		links = resourceDAO.getLinkList(page);		return SUCCESS;
	}
	/*
	 * 修改我的链接
	 */
	public String modifyMineLink() {
		ResourceDAO resourceDAO = new ResourceImple();
		int result = resourceDAO.modifyLink(link);
		if(result == 0) {  // 失败
			
		}else {  //成功
			
		}
		resources = resourceDAO.getResourcelist(link.getLinker().getId());
		links = resourceDAO.getLinkList(link.getLinker().getId());
		return SUCCESS;
	}
	/*
	 * 删除链接
	 */
	public String deleteLink() {
		ResourceDAO resourceDAO = new ResourceImple();
		int result = resourceDAO.deleteLink(link.getId());
		if(result == 0) {  // 失败
			
		}else {  //成功
			
		}
		if (page == null) {
			page = new Page();
			page.setCurrentPage(1);
		}
		page = page.getPage("T_Links where state = 0", page.getCurrentPage(), 20); // 获得分页对象
		links = resourceDAO.getLinkList(page);
		return SUCCESS;
	}
	
	/*
	 * 删除我的链接
	 */
	public String deleteMineLink() {
		ResourceDAO resourceDAO = new ResourceImple();
		int result = resourceDAO.deleteLink(link.getId());
		if(result == 0) {  // 失败
			
		}else {  //成功
			
		}
		resources = resourceDAO.getResourcelist(link.getLinker().getId());
		links = resourceDAO.getLinkList(link.getLinker().getId());
		return SUCCESS;
	}
	
	public Link getLink() {
		return link;
	}

	public void setLink(Link link) {
		this.link = link;
	}

	public void setLinks(List<Link> links) {
		this.links = links;
	}

	public List<Link> getLinks() {
		return links;
	}

	public List<Resource> getResources() {
		return resources;
	}

	public void setResources(List<Resource> resources) {
		this.resources = resources;
	}
	

	public Resource getResource() {
		return resource;
	}

	public void setResource(Resource resource) {
		this.resource = resource;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public Page getPage() {
		return page;
	}

	public void setPage(Page page) {
		this.page = page;
	}

}
